#include <stdio.h>
#include <stdlib.h>
int main(){
int year,month;
scanf("%d%d",&year,&month);
if(year<=0){
   
printf("%d is not year\n",year);
return 1;}
if(month<0 || month>12){
   
   printf("%d is not mouth\n",month);
return 1;
}
int days;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
days = (year%4==0 && year%100!=0 || year%400==0)?29:28;
break;
default:
days = 30;
}
int day = 1;
if(month<3){
 --year;
month += 12;
}
int week = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
printf(" S  M  T  W  T  F  S\n");
for(int i=0;i<week;++i){
printf("   ");
}
for(int i=1;i<=days;++i){
printf("%2d ",i);
if((i+week)%7==0){
printf("\n");
}
}
printf("\n");

//printf("%d %d %d\n",year,month,week);
}
